package cn.tannn.ops.message.controller;

import cn.tannn.jdevelops.annotations.web.mapping.PathRestController;
import cn.tannn.jdevelops.exception.built.BusinessException;
import cn.tannn.jdevelops.jpa.constant.SQLOperator;
import cn.tannn.jdevelops.jpa.result.JpaPageResult;
import cn.tannn.jdevelops.result.response.ResultPageVO;
import cn.tannn.jdevelops.result.response.ResultVO;
import cn.tannn.ops.common.message.MessageSsl;
import cn.tannn.ops.message.constant.WebHookApp;
import cn.tannn.ops.message.controller.pojo.WebHookAdd;
import cn.tannn.ops.message.controller.pojo.WebHookEdit;
import cn.tannn.ops.message.controller.pojo.WebHookPage;
import cn.tannn.ops.message.entity.WebHook;
import cn.tannn.ops.message.service.WebHookService;
import cn.tannn.ops.nginx.service.DomainsService;
import cn.tannn.ops.util.WebHookUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * webhook
 * <p> 钉钉；https://open.dingtalk.com/document/orgapp/custom-bot-to-send-group-chat-messages
 * @author <a href="https://t.tannn.cn/">tan</a>
 * @version V1.0
 * @date 2024/8/23 上午10:37
 */
@PathRestController("/msg/webhook")
@Tag(name = "webhook管理")
@Slf4j
@RequiredArgsConstructor
public class WebHookController {

    private final WebHookService webHookService;
    private final DomainsService domainsService;

    @GetMapping("/apps")
    @Operation(summary = "webhook支持的app", description = "具体说明看代码注释")
    public ResultVO<List<Map<String, String>>> apps() {
        List<Map<String, String>> values = WebHookApp.toMap();
        return ResultVO.success(values);
    }

    // https://open.dingtalk.com/document/orgapp/custom-bot-to-send-group-chat-messages
    @GetMapping("/testMsg")
    @Operation(summary = "发消息测试")
    public ResultVO<String> testMsg(@RequestParam("id") Long id) {
        Optional<WebHook> only = webHookService.findOnly("id", id);
        if(only.isPresent()){
            return WebHookUtil.sendMessage(only.get(), new MessageSsl("这是一条测试数据"));
        }else {
            return ResultVO.failMessage("请检查webhook后重试");
        }

    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取详情", description = "info")
    public ResultVO<WebHook> info(@PathVariable("id") Long id) {
        WebHook bean = webHookService.findOnly("id", id).orElse(new WebHook());
        return ResultVO.success(bean);
    }

    @Operation(summary = "新增webhook")
    @PostMapping("append")
    public ResultVO<String> append(@RequestBody @Valid WebHookAdd append) {
        webHookService.saveOneByVo(append);
        return ResultVO.success();
    }

    @Operation(summary = "分页查询webhook")
    @PostMapping("page")
    public ResultPageVO<WebHook, JpaPageResult<WebHook>> page(@RequestBody @Valid WebHookPage page) {
        Page<WebHook> byBean = webHookService.findPage(page, page.getPage());
        return ResultPageVO.success(JpaPageResult.toPage(byBean), "查询成功");
    }

    @Operation(summary = "webhook集合")
    @GetMapping("lists")
    public ResultVO<List<WebHook>> lists() {
        List<WebHook> finds = webHookService.finds();
        return ResultVO.success(finds);
    }


    @Operation(summary = "查询指定app的webhook集合")
    @GetMapping("lists/app")
    public ResultVO<List<WebHook>> lists(@RequestParam("app") WebHookApp app) {
        List<WebHook> finds = webHookService.finds("app", SQLOperator.EQ, app);
        return ResultVO.success(finds);
    }


    @Operation(summary = "删除webhook")
    @GetMapping("delete")
    @Parameter(name = "id", description = "id", required = true)
    public ResultVO<String> delete(@RequestParam("id") Long id) {
        domainsService.findOnly("webhookId", id)
                .ifPresent(exit -> {throw new BusinessException("当前webHook已被添加到domain,不允许删除");});
        webHookService.deleteEq("id", id);
        return ResultVO.success();
    }

    @Operation(summary = "编辑webhook")
    @PostMapping("edit")
    public ResultVO<String> edit(@RequestBody @Valid WebHookEdit edit) {
        webHookService.update(edit, SQLOperator.EQ);
        return ResultVO.success();
    }


}
